package com.gxa.weixing.pc.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.weixing.core.common.JsonResult;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.core.pojo.Supplier;
import com.gxa.weixing.pc.service.SupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author lijianmin
 * @date 2022/01/06  10:35:50
 */
@RestController
@RequestMapping("/supplier")
@Api(tags = "供应商管理Api")
public class SupplierController {

    @Resource
    private SupplierService supplierService;

    /**
     * 根据特定条件获取供应商列表的方法
     *
     * @param map 条件
     *            name: 供应商名称 模糊
     *            principal:负责人名称 模糊
     *            state: 状态
     *            principalPhone: 负责人手机号 模糊
     *            site: 地址 模糊
     *            startTime: 入驻时间的起止时间
     *            endTime: 入驻时间的起止时间
     *            page: 页码
     *            size: 每页数据量
     * @return 返回信息
     */
    @PostMapping("/getList")
    @ApiOperation("根据特定条件获取供应商列表的接口")
    @ApiImplicitParam(name = "map", value = "编号", paramType = "body")
    public Map<String, Object> getList(@RequestBody Map<String, Object> map) throws BusinessException {
        System.out.println(map);
        System.out.println("----mapmapmapmap-----------------------");
        PageInfo<Supplier> list = supplierService.getList(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(list.getList());
        jsonResult.put("count", list.getTotal());
        return jsonResult.getDataMap();
    }

    /**
     * 修改供应商接口
     *
     * @param supplier 供应商对象
     * @return 返回是否成功
     */
    @PostMapping("/update")
    @ApiOperation("修改供应商接口")
    @ApiImplicitParam(name = "supplier", value = "供应商对象", paramType = "body")
    public Map<String, Object> update(@RequestBody Supplier supplier) throws BusinessException {
        supplierService.update(supplier);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }


    /**
     * 删除供应商接口
     *
     * @param id 删除供应商的编号
     * @return 返回是否成功
     */
    @PostMapping("/delete")
    @ApiOperation("删除供应商接口")
    @ApiImplicitParam(name = "id", value = "编号", paramType = "query")
    public Map<String, Object> delete(@RequestParam("id") Long id) throws BusinessException {
        supplierService.delete(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getDataMap();
    }

    /**
     * 添加供应商接口
     *
     * @param supplier 添加供应商的对象
     * @return 返回是否成功
     */
    @PostMapping("/add")
    @ApiOperation("添加供应商接口")
    @ApiImplicitParam(name = "supplier", value = "供应商对象", paramType = "body")
    public Map<String, Object> add(@RequestBody Supplier supplier) throws BusinessException {
        supplierService.add(supplier);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getDataMap();
    }


}
